module.exports = {
	/**
	 * APP端 手机一键登录
	 * @url user/pub/loginByUniverify 前端调用的url参数地址
	 * @param {String} access_token 			uni.login登录成功后，返回的access_token参数
	 * @param {String} openid 						uni.login登录成功后，返回的openid参数
	 * @param {String} type 							指定操作类型，可选值为login、register，不传此参数时表现为手机号已注册则登录，手机号未注册则进行注册
	 * @param {String} password 					密码，type为register时生效
	 * @param {String} inviteCode 				邀请人的邀请码，type为register时生效
	 * @param {String} myInviteCode 			设置当前注册用户自己的邀请码，type为register时生效
	 * @param {Boolean} needPermission 	设置为true时会在checkToken时返回用户权限（permission），建议在管理控制台中使用
	 * res 返回参数说明
	 * @param {Number} code			错误码，0表示成功
	 * @param {String} msg				详细信息
	 * @param {String} uid 			当前token对应的用户uid
	 * @param {String} type			操作类型，login为登录、register为注册
	 * @param {String} mobile		登录者手机号
	 * @param {String} userInfo	用户全部信息
	 * @param {String} token			登录成功之后返回的token信息
	 * @param {String} tokenExpired		token过期时间
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code : 0, msg : '' };
		// 业务逻辑开始-----------------------------------------------------------
		let info = await uniID.loginByUniverify(data);
		if(info.token){
			let user = {}
			if(info.type === "register"){
				var score = 0
				//查询积分配置
				var signScore = await vk.baseDao.findByWhereJson({
					dbName: 'bus-wx-config',
					whereJson: {
						config_key: 'score_config'
					}
				});
				if(signScore && signScore.register_score > 0){
					score = signScore.register_score
					// 添加积分记录
					var scoreNum = await vk.baseDao.add({
						dbName: "uni-id-scores",
						dataJson: {
							user_id: info.uid,
							type: 1,
							score: score,
							balance: score,
							comment: '首次注册'
						}
					});
				}
				// 邀请码
				// var inviter_uid
				// var inviter_time
				// if(data.inviteCode){
				// 	inviter_uid = data.inviteCode
				// 	inviter_time = new Date().getTime()
				// 	res.register = true
				// }
				let mobileStr = info.mobile.substring(7);
				user = await vk.baseDao.updateById({
					dbName: 'uni-id-users',
					id: info.uid,
					dataJson: {
						nickname: `尾号${mobileStr}用户`,
						gender: 0,
						avatar: 'https://wallpaper-yun.oss-cn-beijing.aliyuncs.com/wall/2021/09/09/7788650722656565-logo.png',
						status: 0,
						score: score,
						// inviter_uid,
						// inviter_time,
						comment: 'app'
					},
					getUpdateData:true
				});
				
				res.msg = '注册成功'
			}else{
				user = await vk.baseDao.findById({
					dbName: 'uni-id-users',
					id: info.uid,
					// dataJson: {
					// 	nickname: data.nickname,
					// 	gender: data.gender,
					// 	avatar: data.avatarUrl,
					// },
					getUpdateData:true
				});
				res.msg = '登录成功'
			}
			
			res.userInfo = user
			res.vk_uni_token = info.vk_uni_token
			res.token= info.token
			res.tokenExpired= info.tokenExpired
			// 日志服务
			const loginLogService = vk.require("service/user/util/login_log");
			await loginLogService.add({
				type: "login",
				login_type: "univerify",
				user_id: info.uid,
				context: originalParam.context
			},util);
		}
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
